package org.budo.jdbc.driver;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.budo.jdbc.driver.exception.BudoJdbcDriverNotSupportedException;
import org.budo.jdbc.driver.resultset.impl.ObjectArrayResultSet;
import org.budo.jdbc.driver.statement.JdbcStatement;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * @author li
 */
@Getter
@Setter
@Accessors(chain = true)
public abstract class AbstractBudoJdbcPreparedStatement extends AbstractBudoJdbcStatement implements PreparedStatement {
    public static final Object NULL = new Object() {
        public String toString() {
            return "null";
        }
    };

    private JdbcStatement jdbcStatement;

    private ResultSet resultSet;

    private Integer updateCount;

    private Map<Integer, Object> parameterMap = Collections.synchronizedMap(new LinkedHashMap<Integer, Object>());

    private int queryTimeout = 60;

    // 是否返回底层数据库的自增主键,默认否
    private int autoGeneratedKeys = Statement.NO_GENERATED_KEYS;

    private ResultSet generatedKeys;

    @Override
    public ResultSet getGeneratedKeys() throws SQLException {
        if (null != this.generatedKeys) {
            return this.generatedKeys;
        }

        return new ObjectArrayResultSet<Object>();
    }

    public Map<Integer, Object> getParameterMap() {
        return Collections.unmodifiableMap(this.parameterMap);
    }

    @Override
    public int getQueryTimeout() throws SQLException {
        return this.queryTimeout;
    }

    @Override
    public void setQueryTimeout(int queryTimeout) throws SQLException {
        this.queryTimeout = queryTimeout;
    }

    /**
     * 复用PreparedStatement时可能用到，pscache
     */
    @Override
    public void clearParameters() throws SQLException {
        this.parameterMap.clear();

        this.resultSet = null;
        this.updateCount = null;
    }

    @Override
    public void close() throws SQLException {
        super.close();

        this.parameterMap = null;

        this.resultSet = null;
        this.updateCount = null;
    }

    @Override
    public boolean execute() throws SQLException {
        JdbcStatement jdbcStatement = this.getJdbcStatement();
        jdbcStatement.execute(this);

        if (null != this.resultSet && null == this.updateCount) {
            return true;
        }

        if (null != this.updateCount && null == this.resultSet) {
            return false;
        }

        throw new RuntimeException("#127 resultSet=" + resultSet + ", updateCount=" + updateCount + ", this=" + this);
    }

    @Override
    public ResultSet executeQuery() throws SQLException {
        this.execute();

        return this.getResultSet();
    }

    @Override
    public int executeUpdate() throws SQLException {
        Connection connection = this.getConnection();
        if (connection.isReadOnly()) {
            throw new SQLException("#84 Connection is read-only. Queries leading to data modification are not allowed, connection=" + connection);
        }

        this.execute();
        return this.getUpdateCount();
    }

    @Override
    public ResultSet getResultSet() throws SQLException {
        if (null == this.resultSet) {
            throw new RuntimeException("#146 resultSet=" + this.resultSet + ", this=" + this);
        }

        return this.resultSet;
    }

    @Override
    public int getUpdateCount() throws SQLException {
        if (null == this.updateCount) {
            throw new RuntimeException("#155 updateCount=" + this.updateCount + ", this=" + this);
        }

        return this.updateCount;
    }

    public void setNull(int parameterIndex, int sqlType) throws SQLException {
        this.setObject(parameterIndex, NULL);
    }

    @Override
    public void setObject(int parameterIndex, Object x) throws SQLException {
        this.parameterMap.put(parameterIndex, x);
    }

    @Override
    public void setString(int parameterIndex, String x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    @Override
    public void setLong(int parameterIndex, long x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    @Override
    public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    @Override
    public void setInt(int parameterIndex, int x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    @Override
    public void setDouble(int parameterIndex, double x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    @Override
    public void setFloat(int parameterIndex, float x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    @Override
    public void setBytes(int parameterIndex, byte[] x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    public void setCharacterStream(int parameterIndex, Reader x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    public void setNCharacterStream(int parameterIndex, Reader x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    @Override
    public void setByte(int parameterIndex, byte x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    @Override
    public void setBoolean(int parameterIndex, boolean x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    @Override
    public void setShort(int parameterIndex, short x) throws SQLException {
        this.setObject(parameterIndex, x);
    }

    @Override
    public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setFetchDirection(int direction) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setFetchSize(int rows) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public int getFetchSize() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public boolean isPoolable() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setDate(int parameterIndex, Date x) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setTime(int parameterIndex, Time x) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void addBatch() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setRef(int parameterIndex, Ref x) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setBlob(int parameterIndex, Blob x) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setClob(int parameterIndex, Clob x) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setArray(int parameterIndex, Array x) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public ResultSetMetaData getMetaData() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setURL(int parameterIndex, URL x) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public ParameterMetaData getParameterMetaData() throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setRowId(int parameterIndex, RowId x) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setNString(int parameterIndex, String value) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setNClob(int parameterIndex, NClob value) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setClob(int parameterIndex, Reader reader) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    @Override
    public void setNClob(int parameterIndex, Reader reader) throws SQLException {
        throw new BudoJdbcDriverNotSupportedException();
    }

    public String toString() {
        return super.toString() + ", jdbcStatement=" + this.getJdbcStatement() + ", parameterMap=" + this.getParameterMap();
    }
}